package cn.stylefeng.guns.modular.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjActivityApply;
import cn.stylefeng.guns.modular.entity.IdjActivityApplyInfo;
import cn.stylefeng.guns.modular.entity.responseVo.ActivityApplyVo;
import cn.stylefeng.guns.modular.entity.responseVo.ActivityCountVo;
import cn.stylefeng.guns.modular.entity.responseVo.ApplyVo;
import cn.stylefeng.guns.modular.mapper.IdjActivityApplyInfoMapper;
import cn.stylefeng.guns.modular.mapper.IdjActivityApplyMapper;
import cn.stylefeng.guns.modular.service.IIdjActivityApplyService;
import cn.stylefeng.guns.modular.util.json.ArrayUtil;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * <p>
 * 活动报名 服务实现类
 * </p>
 *
 * @author TANGWJ
 * @since 2020-08-04
 */
@Service
public class IdjActivityApplyServiceImpl extends ServiceImpl<IdjActivityApplyMapper, IdjActivityApply> implements IIdjActivityApplyService {

	@Autowired
	private IdjActivityApplyMapper mapper;
	
	@Autowired
	private IdjActivityApplyInfoMapper infomapper;
	
	@Override
	public LayuiPageInfo getList(Page<IdjActivityApply> page, String userName, String division, Integer userType,
			String beginTime, String endTime, Integer status, Integer sex, String orderBy, String order) {
		Page<Map<String,Object>> list = mapper.getList(page, userName, division, userType, beginTime, endTime, status, sex, orderBy, order);
		return LayuiPageFactory.createPageInfo(list);
	}

	@Override
	public ModelAndView edit(Long activityId) {
		IdjActivityApply apply = mapper.selectByActivityId(activityId);
		if(apply != null){
			if(ToolUtil.isNotEmpty(apply.getUserType())){
				if(apply.getUserType() == 1){
					apply.setUserTypeName("个人");
				}else{
					apply.setUserTypeName("组合");
				}
			}
			if(ToolUtil.isNotEmpty(apply.getServiceType())){
				if(apply.getServiceType() == 1){
					apply.setServiceTypeName("嘉宾演出");
				}else{
					apply.setServiceTypeName("驻场演出");
				}
			}
			if(apply.getSex() == 1){
				apply.setSexName("男");
			}else if(apply.getSex() == 2){
				apply.setSexName("女");
			}else{
				apply.setSexName("未知");
			}
			if(ToolUtil.isNotEmpty(apply.getIntroductionWork())){
				apply.setActivityList(ArrayUtil.getListByJSONArray(ActivityApplyVo.class, apply.getIntroductionWork()));
			}
		}
		List<IdjActivityApplyInfo> list = infomapper.selectByApplyIdList(activityId);
		if(list.size() > 0){
			List<ApplyVo> applyList = new ArrayList<ApplyVo>();
			for (int i = 0; i < list.size(); i++) {
				if(ToolUtil.isNotEmpty(list.get(i).getCertificateInfo())){
					ApplyVo vo = new ApplyVo();
					JSONObject object = (JSONObject) JSONObject.parse(list.get(i).getCertificateInfo());
					vo.setCertificateType(object.getString("certificateType"));
					vo.setPositive(object.getString("positive"));
					vo.setObverse(object.getString("obverse"));
					applyList.add(vo);
				}
				apply.setApplyList(applyList);
			}
		}
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("apply", apply);
        return modelAndView;
	}

	@Override
	public int setStatus(IdjActivityApply apply) {
		return mapper.setStatus(apply);
	}

	@Override
	public List<ActivityCountVo> selectDivisionCount() {
		return mapper.selectDivisionCount();
	}

	@Override
	public ActivityCountVo selectSumCount() {
		return mapper.selectSumCount();
	}
}
